package com.maxlen.camunda.controller;

import com.maxlen.camunda.constant.CamundaConstant;
import com.maxlen.camunda.entity.BpmnEntity;
import com.maxlen.camunda.entity.FormEntity;
import com.maxlen.camunda.entity.dto.CountByBpmnIdDTO;
import com.maxlen.camunda.entity.dto.InsertBpmnDTO;
import com.maxlen.camunda.entity.dto.UpdateBpmnDTO;
import com.maxlen.camunda.entity.vo.BpmnInfoVO;
import com.maxlen.camunda.service.BpmnService;
import com.tcloudit.common.core.web.domain.AjaxResult;
import com.tcloudit.common.core.web.domain.PageEntity;
import com.tcloudit.common.core.web.page.TableDataInfo;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/camunda/bpmn")
public class BpmnController {

    @Autowired
    private BpmnService bpmnService;

    /**
     * 添加流程模型
     * @param insertBpmnDTO 参数
     * @return 结果
     */
    @PostMapping
    public AjaxResult insertProcessBpmn(@Validated @RequestBody InsertBpmnDTO insertBpmnDTO){
        int rows = bpmnService.insertProcessBpmn(insertBpmnDTO);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 检查模型标识是否唯一
     * @param bpmnId 模型标识
     * @return 结果
     */
    @GetMapping("checkBpmnIdUnique/{bpmnId}")
    public AjaxResult checkBpmnIdUnique(@PathVariable String bpmnId){
        String rows = bpmnService.checkBpmnIdUnique(bpmnId);
        return AjaxResult.success("", rows);
    }

    /**
     * 修改流程模型
     * @param updateBpmnDTO 参数
     * @return 结果
     */
    @PutMapping
    public AjaxResult updateProcessBpmn(@Validated @RequestBody UpdateBpmnDTO updateBpmnDTO){
        int rows = bpmnService.updateProcessBpmn(updateBpmnDTO);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 删除流程模型
     * @param ids 模型ID集合
     * @return 结果
     */
    @DeleteMapping("/{ids}")
    public AjaxResult deleteProcessBpmn(@PathVariable String[] ids){
        int rows = bpmnService.deleteProcessBpmn(ids);
        return rows > 0 ? AjaxResult.success() : AjaxResult.error();
    }

    /**
     * 获取流程模型列表
     * @param page 分页搜索参数
     * @return 结果
     */
    @GetMapping("/list")
    public AjaxResult selectProcessBpmn(@Validated PageEntity<String> page) {
        TableDataInfo list = bpmnService.selectProcessBpmn(page);
        return AjaxResult.success(list);
    }

    /**
     * 获取流程模型详情
     * @param id 流程模型ID
     * @return 结果
     */
    @GetMapping("/{id}")
    public AjaxResult selectProcessBpmnInfo(@PathVariable String id) {
        BpmnInfoVO bpmnInfoVO = bpmnService.selectProcessBpmnInfo(id);
        return AjaxResult.success(bpmnInfoVO);
    }

    /**
     * 根据 id 获取模型文件
     * @param bpmnId 模型标识
     * @return 结果
     */
    @GetMapping("/getBpmnFile/{bpmnId}")
    public AjaxResult getBpmnFile(@PathVariable String bpmnId) throws Throwable {
        String file = bpmnService.getBpmnFile(bpmnId);
        return AjaxResult.success("", file);
    }

    /**
     * 根据 id 保存修改模型文件
     * @param file xml文件
     * @param bpmnId 模型标识
     * @return 结果
     */
    @PostMapping("/saveBpmn" )
    public AjaxResult saveBpmn(MultipartFile file, String bpmnId) {
        bpmnService.saveBpmn(file, bpmnId);
        return AjaxResult.success();
    }
}
